package com.gxuwz.medical.tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.user.User;

public class UserDao {
	//添加用户的操作
		public boolean insert(User user) throws SQLException{
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			try{
				//获取数据库连接
				conn=DbUtil.getConn();
				//获取statement对象
				stmt=conn.createStatement();
				//发送sql语句
				String sql="insert into t_user(user_id,pwd,full_name)value('"+user.getUserid()+"','"+user.getPwd()+"','"+user.getFullname()+"')";
				int num=stmt.executeUpdate(sql);
				if(num>0){
					return true;
				}
				return false;
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				DbUtil.close(rs, stmt, conn);
			}
			return false;
		}
		public ArrayList<User>findAll() throws SQLException{
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			ArrayList<User>list=new ArrayList<User>();
			try{
				//获取数据库连接
				conn=DbUtil.getConn();
				//获取statement对象
				stmt=conn.createStatement();
				//发送sql语句
				String sql="SELECT*FROM t_user";
				rs=stmt.executeQuery(sql);
				while(rs.next()){
					User user=new User();
					user.setUserid(rs.getInt("user_id"));
					user.setPwd(rs.getString("pwd"));
					user.setFullname(rs.getString("full_name"));
					list.add(user);
				}
				return list;
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				DbUtil.close(rs, stmt, conn);
			}
			return null;
		}
		public User find(int id) throws SQLException{
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			try{
				//获取数据库连接
				conn=DbUtil.getConn();
				//获取statement对象
				stmt=conn.createStatement();
				//发送sql语句
				String sql="SELECT*FROM t_user where user_id="+id+"";
				rs=stmt.executeQuery(sql);
				
				//处理结果集
				while(rs.next()){
					User user=new User();
					user.setUserid(rs.getInt("user_id"));
					user.setPwd(rs.getString("pwd"));
					user.setFullname(rs.getString("full_name"));
					return user;
				}
				return null;
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				DbUtil.close(rs, stmt, conn);
			}
			return null;
		}
		/*
		 * 删除用户
		 */
		public boolean delete(int id) throws SQLException{
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			try{
				//获取数据库连接
				conn=DbUtil.getConn();
				//获取statement对象
				stmt=conn.createStatement();
				//发送sql语句
				String sql="delete from t_user where user_id="+id+"";
				int num=stmt.executeUpdate(sql);
				if(num>0){
					return true;
				}
				return false;
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				DbUtil.close(rs, stmt, conn);
			}
			return false;
		}
		public boolean update(User user) throws SQLException{
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			try{
				//获取数据库连接
				conn=DbUtil.getConn();
				//获取statement对象
				stmt=conn.createStatement();
				//发送sql语句
				String sql="UPDATE t_user set pwd='"+user.getPwd()+"',full_name='"+user.getFullname()
						+"'  where user_id='"+user.getUserid()+"'";
				int num=stmt.executeUpdate(sql);
				if(num>0){
					return true;
				}
				return false;
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				DbUtil.close(rs, stmt, conn);
			}
			return false;
		}
}
